*****************************************************************************
*			          Figures Descriptives with ESS	       				*
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 9 2024   										*
* Version:			Stata 17 												*                                                                          
*																			*
*****************************************************************************
/*
This do-file:
- Creates Figure A2 using data from the ESS. 

Input:
- Data\Appendix_ESS.dta

Output:
- Figure A2: Share routine and non-routine 2002-2018 [Figure\Share routine and non routine.pdf]

*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

*Calling the data
use "Data\Appendix_ESS.dta", clear 

*******************************************************************************
* Graphs
*******************************************************************************
* Graph style
{
grstyle clear
set scheme s2color
grstyle init
grstyle set plain, box
grstyle color background white
grstyle color major_grid gs8
grstyle linepattern major_grid dot
}
// Figure A2: Share routine and non-routine 2002-2018
{
preserve
// Preserve the current dataset in memory to allow restoration later

// Generate a binary variable 'tokeep' to identify the countries to be kept for Figure A2
gen tokeep = (cou == "BEL" | cou == "CZE" | cou == "EST" | cou == "HUN" | cou == "LUX" | cou == "SVK" | cou == "SVN" | cou == "DNK" | cou == "FIN" | cou == "FRA" | cou == "DEU" | cou == "GRC" | cou == "IRL" | cou == "NLD" | cou == "PRT" | cou == "ESP" | cou == "SWE" | cou == "GBR" | cou == "NOR" | cou == "CHE" | cou == "AUT" | cou == "ITA" | cou == "POL")

// Keep only the observations for the countries specified in 'tokeep'
keep if tokeep == 1


// Keep observations where 'mnactic' is 1 or 3 (paid work or unemployed looking for a job), and 'rti' is not missing
keep if inlist(mnactic, 1, 3) & rti ~= .

// Generate a binary variable 'routine' to indicate whether the task is routine (rti > 0)
gen routine = 1 if rti > 0
replace routine = 0 if rti < 0

// Calculate the total number of people in each occupation per year for each country and routine category
bysort year routine cou: egen empl = sum(dweight)

// Calculate the total number of people in each ISCO2 category per year for each country
bysort year cou: egen tot = sum(dweight)

// Calculate the share of people within each type of task (routine or non-routine) per year
gen share = empl / tot * 100

// Label the variables with descriptive names
label var empl "total no people within occup/year"
label var tot "total no people with isco2 within year"
label var share "share of people within type of task routinary or not /year"
lab var cou "Country"

// Collapse the dataset to calculate the weighted share of routine and non-routine tasks by year and country
collapse share [aw=pweight], by(routine year cou)

// Sort the dataset by country, year, and routine status
sort cou year routine share

// Generate separate variables for the share of non-routine and routine tasks
gen routine0 = share if routine == 0
gen routine1 = share if routine == 1

// Calculate the total share of non-routine tasks per year and country
bysort year cou: egen share0 = total(routine0)

// Calculate the total share of routine tasks per year and country
bysort year cou: egen share1 = total(routine1)

// Calculate the polarization between non-routine and routine tasks
gen polarization = share0 - share1

// Generate a line graph showing the share of routine and non-routine tasks over time for each country
graph twoway ///
    line share year if routine == 1, lc(red) legend(label(1 "Routine")) by(cou) || ///
    line share year if routine == 0, lc(blue) legend(label(2 "Non-Routine")) by(cou)

// Export the graph to a PDF file
graph export "Figure\Share routine and non routine.pdf", as(pdf) replace

// Restore the original dataset that was in memory before any modifications
restore
	
}